/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.tp2.etna.exportacion.dao;

import com.tp2.etna.exportacion.modelo.DocumentoSicexModel;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class DocumentoSicexDAOImpl implements
        DocumentoSicexDAO {

    @Autowired
    private DataSource dataSource;

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public List<DocumentoSicexModel> listarDocumentosSicex() {
        List<DocumentoSicexModel> documentos = new ArrayList<DocumentoSicexModel>();
        String query = "select Codigo, Descripcion, FechaRegistro "
                + " From documentosicex;";

        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            con = dataSource.getConnection();
            ps = con.prepareStatement(query);
            rs = ps.executeQuery();
            while (rs.next()) {
                DocumentoSicexModel itm = new DocumentoSicexModel();
                itm.setCodigo(rs.getInt("Codigo"));
                itm.setDescripcion(rs.getString("Descripcion"));
                itm.setFechaRegistro(rs.getString("FechaRegistro"));
                documentos.add(itm);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                rs.close();
                ps.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return documentos;
    }
    
}
